<template>
  <a-modal :open="open" width="800px" :title="rowData?.brId ? '客户档案' : '客户档案'" centered @cancel="onCancel">
    <div class="w-full overflow-auto pr-2.5" style="height: 500px">
      <PatientInfoForm :key="componentsKey" v-model:value="formState" />
    </div>
    <template #footer>
      <a-button @click="onClickReset()">重置</a-button>
      <a-button type="primary" @click="onSave(1)">保存</a-button>
      <a-button v-if="isSave" type="primary" @click="onSave(2)">保存并登记</a-button>
    </template>
    <RegisterModal v-model:open="editOpen" :row-data="{ ...formState }" title="挂号" @success="onSuccess" />
  </a-modal>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import cofirmModal from '@/utils/confirm.js'
import { getSystemConfig } from '@/utils/systemConfig'
import RegisterModal from '@/components/RegisterModal'

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  },
  isSave: {
    type: Boolean,
    default: () => false
  }
})
const emit = defineEmits(['update:open', 'success'])
const loading = ref(true)
const editOpen = ref(false)
const componentsKey = ref(Math.random())
const isRegister = ref()
const formState = ref({})
const onCancel = () => {
  emit('update:open', false)
}
const onSuccess = () => {
  componentsKey.value = Math.random()
  emit('update:open', false)
  emit('success')
}
const onSave = async (state) => {
  // 验证：检查是否提供了必要的患者信息
  if (!formState.value.brName || !formState.value.pyjm || !formState.value.brAge) {
    return message.error('请填写患者信息')
  }
  // 验证：如果类型为 '身份证'，检查是否提供了身份证，并对其进行验证
  const idCard = formState.value.brSfz
  if (formState.value.brZjlx === '身份证' && idCard && idCard.length) {
    const { code: codeZjh, msg: msgZjh } = await post('/common/util/getCsrq', { sfzhm: idCard })
    if (codeZjh !== 0) {
      return message.error(msgZjh || '身份证有误')
    }
  }
  // 根据条件设置 jksy 值
  if (formState.value.jksy !== 0) {
    formState.value.jksy = formState.value?.brId ? 2 : 1
  }
  // 保存患者信息
  const res = await post('/common/patient/savePatientInfo', formState.value)
  if (res.code === 0) {
    // 根据条件打开编辑模式或跳转到注册页面
    if (state === 2) {
      formState.value.brId = res.data
      // if (isRegister.value) {
      //   editOpen.value = true
      // } else {
      //   router.push({
      //     path: '/out-registration/register',
      //     query: {
      //       id: formState.value.brId
      //     }
      //   })
      // }
      const { code, msg } = await post('/outp/mzdj/doCheckIn', { ...formState.value, sfdjjr: '1' })
      if (code !== 0) return message.error(msg)
      message.success(msg || '操作成功')
      onSuccess()
    } else {
      onSuccess()
    }
  } else {
    if (props.rowData?.brId) {
      return message.error(res.msg)
    }
    // 弹出错误确认框；如果确认，修改 jksy 值重新保存
    const isConfirm = await cofirmModal({ title: '提示', content: res.msg })
    if (isConfirm) {
      formState.value.jksy = 0
      await onSave(state)
    }
  }
}

const onClickReset = () => {
  if (props.rowData?.brId) {
    formState.value = { ...props.rowData }
  } else {
    // 清空其他项，收费标准默认选中第一个
    formState.value = {}
  }
}
watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.brId) {
        formState.value = { ...props.rowData }
      }
    } else {
      // 清空其他项，收费标准默认选中第一个
      formState.value = {}
    }
  }
)

onMounted(() => {
  setTimeout(() => {
    loading.value = false
  }, 100)
  isRegister.value = getSystemConfig('GHDJYS') === '1' //挂号登记样式
})
</script>
